pub fn condition_control() {
    println!("---- 条件控制语句 -------------");
    // 条件控制语句
    let number = 6;
    if number % 4 == 0 {
        println!("number is divisible by 4");
    } else if number % 3 == 0 {
        println!("number is divisible by 3");
    } else if number % 2 == 0 {
        println!("number is divisible by 2");
    } else {
        println!("number is not divisible by 4, 3, or 2");
    }
    println!("---- 三元表达式 -------------");
    // 三元表达式
    let condition = true;
    let s = if condition { 'a' } else { 'b' }; // 两个条件返回的结果必须类型一致
    println!("{}", s)
}

pub fn loop_control() {
    println!("---- loop_control -------------");
    let mut count = 0;
    'counting_up: loop {
        // 指定最外层loop的标签
        println!("count = {}", count);
        let mut remaining = 10;

        loop {
            println!("remaining = {}", remaining);
            if remaining == 9 {
                break; // 默认退出当前层的循环
            }
            if count == 2 {
                break 'counting_up; // 通过loop的标签直接退出最外层的循环
            }
            remaining -= 1;
        }

        count += 1;
    }
    println!("End count = {}", count);

    println!("---- 从循环返回 -------------");
    // 从循环返回
    let mut counter = 0;

    let result = loop {
        counter += 1;

        if counter == 10 {
            break counter * 2; // 返回值
        }
    };

    println!("The result is {}", result);

    println!("---- while loop -------------");
    // while loop
    let mut number = 3;

    while number != 0 {
        println!("{}!", number);
        number -= 1;
    }
    println!("LIFTOFF!!!");

    println!("---- for each -------------");
    // for each
    let a = [10, 20, 30, 40, 50];

    for element in a {
        println!("the value is: {}", element);
    }

    for (index, &element) in a.iter().enumerate() {
        println!("Index: {}, Value: {}", index, element);
    }

    // .rev() 会倒置数组
    for number in (1..4).rev() {
        println!("{}!", number);
    }
    println!("LIFTOFF!!!");
    // 等效于
    [1, 2, 3]
        .iter()
        .rev()
        .for_each(|element| println!("{}!", element));
    println!("LIFTOFF!!!");
}
